<?php
$template = "config-site";
$reload=false;

function cleanString($string){
    $string = str_replace('http://', '', $string);
    if(substr($string, -1) === '/') $string = substr ($string, 0, -1);
    return $string;
}

// récupération des données en cours
if(DEBUG_MODE && file_exists(CONFIG_PATH.'config_local.ini')){
    $datas = parse_ini_file(CONFIG_PATH.'config_local.ini',true);
}elseif(!DEBUG_MODE && file_exists(CONFIG_PATH.'config.ini')){
    $datas = parse_ini_file(CONFIG_PATH.'config.ini',true);
}else{
    exit;
}

// sauvegarde des données en tmp
if(isset($_POST)&&isset($_POST['submit'])){
    // domaine
    $datas['domaine']['baseURL'] = 'http://'.$_POST['domaine_baseURL'].'/';
    $datas['domaine']['baseDirectory'] = ($_POST['domaine_baseDirectory']!=='')?$_POST['domaine_baseDirectory'].'/':'';
    
    // modification du rewrite
    $stringReWrite = "<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /".$datas['domaine']['baseDirectory']."
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /".$datas['domaine']['baseDirectory']."index.php [L]
    </IfModule>";
    $fileHtaccess = ADMIN_PATH.'../.htaccess';
    $handle = fopen($fileHtaccess,"w+");
    fwrite($handle, $stringReWrite);
    fclose($handle);
    // mode
    $datas['mode']['maintenance'] = (isset($_POST['mode_maintenance']))?1:0;
    
    // google analytics
    $datas['google']['code_analytics'] = $_POST['google_code_analytics'];
    
    // mailjet
    $datas['mailjet']['apikey'] = $_POST['mailjet_apikey'];
    $datas['mailjet']['secretkey'] = $_POST['mailjet_secretkey'];
    $datas['mailjet']['nom'] = $_POST['mailjet_nom'];
    $datas['mailjet']['mail'] = $_POST['mailjet_mail'];
    
    if($_POST['submit']==='Modifier la base' || $_POST['submit']==='Changer la base' ){
        // mise à jour du serveur
        $datas['bdd']['server'] = $_POST['bdd_server'];
        $datas['bdd']['username'] = $_POST['bdd_username'];
        $datas['bdd']['password'] = $_POST['bdd_password'];
        $datas['bdd']['database_name'] = $_POST['bdd_database_name'];

        // test différence de configuration de la base de données
        if($datas['bdd']['database_prefix'] !== $_POST['bdd_database_prefix']){
            if(bdd_sql::changePrefixTable($_POST['bdd_database_prefix'],$datas['bdd']['database_prefix'])){
                PWE::updatePrefixTable($_POST['bdd_database_prefix']);
                $datas['bdd']['database_prefix'] = $_POST['bdd_database_prefix'];
            }
        }
    }
    
    //langues
    if(isset($_POST['langues'])){
        if(isset($datas['langues'])){
            foreach($datas['langues'] as $lang=>$statut){
                if(isset($_POST['langues'][$lang])){
                    $datas['langues'][$lang] = 1;
                }else{
                    $datas['langues'][$lang] = 0;
                }
            }
        }
    }elseif(isset($datas['langues'])){
        foreach($datas['langues'] as $lang=>$statut){
            $datas['langues'][$lang] = 0;
        }
    }
    $config_ini = '';
    foreach($datas as $rub=>$content){
        $config_ini .= '['.$rub.']'."\n";
        foreach($content as $k=>$v){
            $config_ini .= $k.' = "'.$v.'"'."\n";
        }
    }
    
    $fileName = (DEBUG_MODE)?'config_local.ini':'config.ini';
    $handle = fopen(CONFIG_PATH.$fileName,"w+");
    fwrite($handle, $config_ini);
    fclose($handle);
    
    $reload = true;
}

// matching des données du .ini dans le formulaire
if(isset($datas)){
    foreach($datas as $rub=>$content){
        if($rub === 'langues'){
            $_POST[$rub]=$content;
        }else{
            foreach($content as $k=>$v){
                $_POST[$rub.'_'.$k] = cleanString($v);
            }
        }
    }
}

if($reload){
    header('location: ?template='.$template);
    exit;
}
?>
<div class="columns sixteen">
    <form method="post" enctype="multipart/form-data">
        <table class="liste ui-widget" width="100%">
            <thead class="ui-widget-header">
                <tr>
                    <th colspan="2">Paramètres d'installation</th>
                </tr>
            </thead>
            <tbody class="ui-widget-content">
                <tr>
                    <td width="25%">nom de domaine</td>
                    <td>http://<input type="text" name="domaine_baseURL" value="<?php echo (isset($_POST)&&isset($_POST['domaine_baseURL']))?$_POST['domaine_baseURL']:''; ?>"/>/</td>
                </tr>
                <tr>
                    <td width="25%">Site en maintenance</td>
                    <td><input type="checkbox" name="mode_maintenance" <?php echo(isset($_POST)&&isset($_POST['mode_maintenance'])&&$_POST['mode_maintenance']==1)?'checked="checked"':''; ?>/></td>
                </tr>
                <tr>
                    <td>répertoire d'installation</td>
                    <td><input type="text" name="domaine_baseDirectory" value="<?php echo (isset($_POST)&&isset($_POST['domaine_baseDirectory']))?$_POST['domaine_baseDirectory']:''; ?>"/>/</td>
                </tr>
                <tr>
                    <td>Code Google Analytics</td>
                    <td><input type="text" name="google_code_analytics" value="<?php echo (isset($_POST)&&isset($_POST['google_code_analytics']))?$_POST['google_code_analytics']:''; ?>"/></td>
                </tr>
                <tr class="ui-widget-header">
                    <td>Configuration mailjet</td>
                    <td></td>
                </tr>
                <tr>
                    <td>Nom de l'auteur :</td>
                    <td><input type="text" name="mailjet_nom" value="<?php echo (isset($_POST)&&isset($_POST['mailjet_nom']))?$_POST['mailjet_nom']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>mail de l'auteur :</td>
                    <td><input type="text" name="mailjet_mail" value="<?php echo (isset($_POST)&&isset($_POST['mailjet_mail']))?$_POST['mailjet_mail']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>API key :</td>
                    <td><input type="text" name="mailjet_apikey" value="<?php echo (isset($_POST)&&isset($_POST['mailjet_apikey']))?$_POST['mailjet_apikey']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>Secret key :</td>
                    <td><input type="text" name="mailjet_secretkey" value="<?php echo (isset($_POST)&&isset($_POST['mailjet_secretkey']))?$_POST['mailjet_secretkey']:''; ?>"/></td>
                </tr>
                <?php if(isset($_POST)&&isset($_POST['langues'])): ?>
                <tr class="ui-widget-header">
                    <td>Langues</td>
                    <td></td>
                </tr>
                <tr>
                    <td>Langues suplémentaires</td>
                    <td>
                        <ul>
                            <?php foreach($_POST['langues'] as $lang=>$v): ?>
                            <li><?php echo $lang; ?> <input type="checkbox" name="langues[<?php echo $lang; ?>]" <?php echo($v==1)?'checked="checked"':''; ?>/></li>
                            <?php endforeach; ?>
                        </ul>
                    </td>
                </tr>
                <?php endif; ?>
                <tr class="ui-widget-header">
                    <td>Base de données</td>
                    <td align="right">
                        <input id="save_bdd" class="pwe-ui-btn" type="submit" name="submit" value="Modifier la base" style="display: none;"/>
                        <input id="bdd_ok" class="pwe-ui-btn" type="submit" name="submit" value="Changer la base" style="display: none;"/>
                    </td>
                </tr>
                <tr>
                    <td>Serveur</td>
                    <td><input type="text" class="checkBdd" id="bdd_server" name="bdd_server" value="<?php echo (isset($_POST)&&isset($_POST['bdd_server']))?$_POST['bdd_server']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>Nom de la base</td>
                    <td><input type="text" class="checkBdd" id="bdd_database_name" name="bdd_database_name" value="<?php echo (isset($_POST)&&isset($_POST['bdd_database_name']))?$_POST['bdd_database_name']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>Nom d'utilisateur</td>
                    <td><input type="text" class="checkBdd" id="bdd_username" name="bdd_username" value="<?php echo (isset($_POST)&&isset($_POST['bdd_username']))?$_POST['bdd_username']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>Mot de passe</td>
                    <td><input type="password" class="checkBdd" id="bdd_password" name="bdd_password" value="<?php echo (isset($_POST)&&isset($_POST['bdd_password']))?$_POST['bdd_password']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>Prefix de table</td>
                    <td><input type="text" class="checkBdd" id="bdd_database_prefix" name="bdd_database_prefix" value="<?php echo (isset($_POST)&&isset($_POST['bdd_database_prefix']))?$_POST['bdd_database_prefix']:''; ?>"/></td>
                </tr>
                <tr>
                    <td align="center" colspan="2"><p id="error_serveur" class="error"></p></td>
                </tr>
                    
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="2" align="right"><input class="pwe-ui-btn" type="submit" name="submit" value="Enregistrer" /></td>
                </tr>
            </tfoot>
        </table>
    </form>
</div>